/**
 * Created by jinxs on 14-11-11.
 */
(function ($) {
    /**
     * 缓存名称
     * @type {string}
     * @private
     */
    var _cacheName = 'uiOfficeControl';
    /**
     * @class Office控制组件
     * @constructor uiOfficeControl
     * @summary 构造函数
     * @param [options] {Object|String} 组件配置参数|要调用的组件方法名
     * @param [param] {Object} 表示要调用的组件方法的参数
     * @example
     * // 构件组件
     * $('#divMenu').uiOfficeControl({width: 160, height: 500});
     * // 调用组件方法options
     * $('#divMenu').uiOfficeControl("options");
     */
    $.fn.uiOfficeControl = function (options, param) {
        if (typeof options == 'string') {
            return $.fn.uiOfficeControl.methods[options](this, param);
        }
        return this.each(function(){
            var me = $(this);
            var state = me.data(_cacheName);

            options = options || {};
            if (state) {
                $.extend(state.options, options);
            } else {
                me.data(_cacheName, {
                    options: $.extend({}, $.fn.uiOfficeControl.defaults, me.parseUIConfig(), options)
                });
            }
            _init(me);
            _resize(me);
        });
    };
    /**
     * 方法
     * @lends uiOfficeControl.prototype
     */
    $.fn.uiOfficeControl.methods = {
        /**
         * @summary 获取组件配置参数
         * @function
         * @name uiOfficeControl#options
         * @returns {Object}
         * @example
         *  $('#btn').uiOfficeControl('options');
         */
        options: function (target) {
            return $.data(target[0], _cacheName).options;
        },
        /**
         * @summary 获取组件对象
         * @name uiOfficeControl#getCab
         * @function
         * @returns {jQuery} 方法调用者
         */
        getCab: function (target) {
            var targetID = target.getElemID();
            return $.getByParentID(targetID, $.uiTemplate.uiNTOK.NTOK_ID)[0];
        }
    };
    /**
     * 默认配置
     * @lends uiOfficeControl.prototype
     */
    $.fn.uiOfficeControl.defaults =
    {
        /**
         * @type Boolean
         * @default true
         * @summary 是否自适应大小
         */
        autoFit: true,
        /**
         * @type String
         * @default "Office在线文档"
         * @summary 标题
         */
        caption: 'Office在线文档',
        classid: '',
        /**
         * @type String
         * @default ""
         * @summary 版权所有说明
         */
        makerCaption: '',
        /**
         * @type String
         * @default ""
         * @summary 版权所Key
         */
        makerKey: '',
        /**
         * @type String
         * @default ""
         * @summary 产品说明
         */
        productCaption: '',
        /**
         * @type String
         * @default ""
         * @summary 产品Key
         */
        productKey: '',
        /**
         * @type String
         * @default ""
         * @summary 组件所在的URL路径
         */
        cabRoot: '',
        cabName: 'OfficeControl.cab',
        /**
         * @type String
         * @default "5,0,2,8"
         * @summary 组件版本号
         */
        cabVersion: '5,0,2,8',
        /**
         * @type String
         * @default "0"
         * @summary 标题栏的文本颜色
         */
        titlebarTextColor: '0',
        /**
         * @type String
         * @default "16777215"
         * @summary 菜单栏颜色
         */
        menubarColor: '16777215',
        /**
         * @type String
         * @default "16180947"
         * @summary 菜单栏按钮颜色
         */
        menuButtonColor: '16180947',
        /**
         * @type String
         * @default "14540253"
         * @summary 边框颜色
         */
        borderColor: '14540253',
        /**
         * @type String
         * @default "16119285"
         * @summary 标题栏颜色
         */
        titlebarColor: '16119285',
        /**
         * @type String
         * @default "0"
         * @summary 菜单栏风格
         */
        menuBarStyle: '0',
        /**
         * @type String
         * @default "7"
         * @summary 菜单栏按钮风格
         */
        menuButtonStyle: '7',
        /**
         * @type String
         * @default "NTKO"
         * @summary 互联网用户名
         */
        webUserName: 'NTKO',
        /**
         * @type String
         * @default "-1"
         * @summary 是否使用UTF8URL
         */
        isUseUTF8URL: '-1',
        /**
         * @type String
         * @default "-1"
         * @summary 是否使用UTF8Data
         */
        isUseUTF8Data: '-1',
        /**
         * @type String
         * @default "0"
         * @summary 边框风格
         */
        borderStyle: '0'
    };
    //////--------------------------------内部方法------------------------------
    /**
     * 创建
     * @param target 目标对象
     */
    var _init = function (target) {
        var options = $.data(target[0], _cacheName).options;
        target.setElemID();
        var targetID = target.getElemID();
        var html = [];
        html.push('<object id="' + $.getIDByParentID(targetID, $.uiTemplate.uiNTOK.NTOK_ID) + '"');
        html.push(' codebase="' + options.cabRoot + options.cabName + '#version=' + options.cabVersion + '&rand=' + guidGenerator() + '"');
        if (_IE) {
            html.push(' classid="clsid:' + options.classid + '">');
            html.push('<param name="MakerCaption" value="' + options.makerCaption + '"> ');
            html.push('<param name="MakerKey" value="' + options.makerKey + '"> ');
            html.push('<param name="ProductCaption" value="' + options.productCaption + '"> ');
            html.push('<param name="ProductKey" value="' + options.productKey + '">');
            html.push('<param name="Caption" value="' + options.caption + '">');
            html.push('<param name="TitlebarTextColor" value="' + options.titlebarTextColor + '">');
            html.push('<param name="MenubarColor" value="' + options.menubarColor + '">');
            html.push('<param name="MenuButtonColor" VALUE="' + options.menuButtonColor + '">');
            html.push('<param name="BorderColor" value="' + options.borderColor + '">');
            html.push('<param name="TitlebarColor" value="' + options.titlebarColor + '">');
            html.push('<param name="MenuBarStyle" value="' + options.menuBarStyle + '">');
            html.push('<param name="MenuButtonStyle" value="' + options.menuButtonStyle + '">');
            html.push('<param name="WebUserName" value="' + options.webUserName + '">');
            html.push('<param name="IsUseUTF8URL" value="' + options.isUseUTF8URL + '">');
            html.push('<param name="IsUseUTF8Data" value="' + options.isUseUTF8Data + '">');
            html.push('<param name="BorderStyle" value="' + options.borderStyle + '">');
            html.push('<param name="wmode" value="Opaque"> ');
            html.push('<SPAN STYLE="color:red">不能装载文档控件。请在检查浏览器的选项中检查浏览器的安全设置。</SPAN>   ');
        } else {
            html.push('clsid="{' + options.classid + '}"');
            html.push('type="application/ntko-plug"');
            html.push('_MakerCaption="' + options.makerCaption + '"');
            html.push('_wmode="Opaque"');
            html.push('_MakerKey="' + options.makerKey + '"');
            html.push('_ProductCaption="' + options.productCaption + '"');
            html.push('_ProductKey="' + options.productKey + '"');
            html.push('_Caption="' + options.caption + '"');
            html.push('_TitlebarTextColor="' + options.titlebarTextColor + '"');
            html.push('_MenubarColor="' + options.menubarColor + '"');
            html.push('_MenuButtonColor="' + options.menuButtonColor + '"');
            html.push('_BorderColor="' + options.borderColor + '"');
            html.push('_TitlebarColor="' + options.titlebarColor + '"');
            html.push('_MenuBarStyle="' + options.menuBarStyle + '"');
            html.push('_MenuButtonStyle="' + options.menuButtonStyle + '"');
            html.push('_WebUserName="' + options.webUserName + '"');
            html.push('_IsUseUTF8URL="' + options.isUseUTF8URL + '"');
            html.push('_IsUseUTF8Data="' + options.isUseUTF8Data + '"');
            html.push('_BorderStyle="' + options.borderStyle + '"');
            html.push('>');
            if (webUI_browser().chrome) {
                html.push('<SPAN STYLE="color:red">尚未安装Office文档插件。请点击<a href="' + options.cabRoot + 'NtkoCrossBrowserSetup.msi">安装组件</a></SPAN>   ');
            } else if (webUI_browser().firefox) {
                html.push('<SPAN STYLE="color:red">尚未安装Office文档插件。请点击<a href="' + options.cabRoot + 'NtkoCrossBrowserSetup.msi">安装组件</a></SPAN>   ');
            } else {
                html.push('<SPAN STYLE="color:red">不能装载文档控件。浏览器不支持！支持的浏览器：IE、Firefox、Chrome</SPAN>   ');
            }
        }
        html.push('</object>');
        target.append(html.join(''));
        target.bind('_resize', function () {
            _resize(target);
        });
        return target;
    };
    var _resize = function (target) {
        var options = $.data(target[0], _cacheName).options;
        if (options.autoFit) {
            target.autoFit(true, false);
            var cab = $(target.uiOfficeControl('getCab'));
            cab.width(target.width());
            cab.height(target.height());
        }
    };
})(jQuery);
